package com.etone.smartAudit.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.etone.smartAudit.service.FileUploadService;
import com.etone.smartAudit.vo.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-06-16 14:35:30
 * @Description FileController
 */
@AllArgsConstructor
@RestController
@Api(tags = "文件", value = "FileController")
public class FileUploadController {

    private FileUploadService fileUploadService;

    @ApiOperation(value = "上传文件《完》")
    @PostMapping(value = "/upload/local/{type}", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<FileVO> uploadLocalFile(
            @ApiParam(value = "文件类型", required = true) @PathVariable String type,
            @ApiParam(value = "文件", required = true) @RequestPart MultipartFile[] files
    ) {
        return fileUploadService.fileLocalUpload(null, type, files);
    }

    @ApiOperation(value = "下载文件《完》")
    @GetMapping(value = "/upload/files/{type}/{fileName}")
    public void getFile(
            @ApiParam(value = "文件类型", required = true) @PathVariable String type,
            @ApiParam(value = "文件名称", required = true) @PathVariable String fileName,
            HttpServletResponse response
    ) {
        fileUploadService.getFile(null, type, fileName, response);
    }

    @ApiOperation(value = "多级上传文件《完》")
    @PostMapping(value = "/upload/local/{path}/{type}", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<FileVO> uploadLocalFile2(
            @ApiParam(value = "文件路径", required = true) @PathVariable String path,
            @ApiParam(value = "文件类型", required = true) @PathVariable String type,
            @ApiParam(value = "文件", required = true) @RequestPart MultipartFile[] files
    ) {
        return fileUploadService.fileLocalUpload(path, type, files);
    }

    @ApiOperation(value = "多级下载文件《完》")
    @GetMapping(value = "/upload/files/{path}/{type}/{fileName}")
    public void getFile2(
            @ApiParam(value = "文件路径", required = true) @PathVariable String path,
            @ApiParam(value = "文件类型", required = true) @PathVariable String type,
            @ApiParam(value = "文件名称", required = true) @PathVariable String fileName,
            HttpServletResponse response
    ) {
        fileUploadService.getFile(path, type, fileName, response);
    }

    /***
     * 下载模板文件
     */
    @ApiOperation(value = "下载模板文件")
    @GetMapping(value = "/download/template/{fileName}")
    public void downloadTemplateFile(@ApiParam(value = "文件名称", required = true) @PathVariable String fileName,
                                     HttpServletResponse response) {
        String path = "tfs/" +fileName;
        InputStream inputStream = ResourceUtil.getStream(path);
        fileUploadService.getFile(inputStream,response,fileName);
    }

}
